package edu.uoc.tds.pac4.client.ss2;

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;

import edu.uoc.tds.pac4.beans.Event;
import edu.uoc.tds.pac4.beans.TipusEvent;
import edu.uoc.tds.pac4.client.PanelClientInterface;
import edu.uoc.tds.pac4.common.GestioEventsException;
import edu.uoc.tds.pac4.common.PantallaError;
import edu.uoc.tds.pac4.common.TDSLanguageUtils;
import edu.uoc.tds.pac4.interfaces.RemoteInterface;

/**
 * Pantalla per afegir/editar un event
 */
public class PantallaCampsEvent extends PantallaGestorEvents implements
		PanelClientInterface {

	private static final long serialVersionUID = 1L;
	protected static final int defaultHeight = 20;
	private JPanel jPanelContainer = null;
	private JPanel jPanelTitol = null;
	private JLabel jLabelTitolCentre = null;
	private JLabel jLabelNomCentre = null;
	private JLabel jLabelTitolCodi = null;
	private JLabel jLabelCodiCentre = null;
	private JPanel jPanelDadesEvent = null;
	private JLabel jLabelEventTitol = null;
	private JLabel jLabelNomEvent = null;
	private JTextField jTextFieldNomEvent = null;
	private JComboBox jComboBoxTipus = null;
	private JLabel jLabelTipus = null;
	private JLabel jLabelPlaces = null;
	private JLabel jLabelPreu = null;
	private JTextField jTextFieldPlaces = null;
	private JTextField jTextFieldPreu = null;
	private JLabel jLabelTitolDates = null;
	private JLabel jLabelDataInici = null;
	private JTextField jTextFieldDataInici = null;
	private JLabel jLabelDataFi = null;
	private JTextField jTextFieldDataFi = null;
	private JLabel jLabelLlindar = null;
	private JLabel jLabelDurada = null;
	private JTextField jTextFieldLlindar = null;
	private JTextField jTextFieldDurada = null;
	private JLabel jLabelTitolPeriode = null;
	private JPanelFiltreCalendari jPanelFiltre = null;
	private JPanelCalendari jPanelCalendari = null;
	private JPanelEvents jPanelEvents = null;
	private JPanelEvents jPanelPreRequisits = null;
	private JPanel jPanelAccions = null;
	private JPanel jPanelAccionsEvent = null;
	private JButton jButtonAfegir = null;
	private JButton jButtonEliminar = null;
	private JButton jButtonGuardarEvent = null;
	private JButton jButtonCancelar = null;
	private Vector<Event> currentEvents = null;
	private Vector<Event> preRequisits = new Vector<Event>();
	private JTextField jTextFieldDataPeriodeInici = null;
	private JTextField jTextFieldDataPeriodeFi = null;
	private JLabel jLabelDataPeriodeInici = null;
	private JLabel jLabelDataPeriodeFi = null;
	private Event event = null;
	private GestorPantallaSS2 gestor = null;
	private final Dimension dimButton = new Dimension(125, 30);

	/**
	 * Constructor per a crear un nou event
	 * @param remot
	 */
	public PantallaCampsEvent(RemoteInterface remot, GestorPantallaSS2 gestor) {
		super();
		this.remot = remot;
		this.gestor = gestor;
	}

	/**
	 * Constructor per a modificar un event donat
	 * @param remote
	 * @param event
	 */
	public PantallaCampsEvent(RemoteInterface remot, GestorPantallaSS2 gestor, Event event) {
		super();
		this.remot = remot;
		this.gestor = gestor;
		this.event = event;
	}

	/**
	 * Inicialitza la pantalla carregant les dades als camps
	 */
	private void initializeDades() {
		// Mostra les dades de l'event i/o centre
		if(event != null) {
			DateFormat fmt  = new SimpleDateFormat("dd/MM/yyyy");
			
			jTextFieldNomEvent.setText(event.getDescripcio());
			jTextFieldPlaces.setText(String.valueOf(event.getCapacitat()));
			jTextFieldPreu.setText(String.valueOf(event.getPreu()));
			jTextFieldDataInici.setText(fmt.format(event.getDataInici()));
			jTextFieldDataFi.setText(fmt.format(event.getDataFi()));
			jTextFieldDurada.setText(String.valueOf(event.getDurada()));
			jTextFieldLlindar.setText(String.valueOf(event.getPercentatge()));
			jTextFieldLlindar.setEnabled(false);
			jTextFieldDataPeriodeInici.setText(fmt.format(event.getInscripInici()));
			jTextFieldDataPeriodeFi.setText(fmt.format(event.getInscripFi()));
			
			jLabelNomCentre.setText(event.getCentre().getNom());
			jLabelCodiCentre.setText(event.getCentre().getCodi());
			try {
				preRequisits = remot.getPrerequisitsEvent(event);
			} catch (RemoteException e) {
				new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
			} catch (GestioEventsException e) {
				new PantallaError(e.getMessage(), PantallaError.Tipus.ERROR);
			}
			for(int i=0;i<jComboBoxTipus.getItemCount();i++)
				if(jComboBoxTipus.getItemAt(i).toString().equals(event.getTipusevent().getNom())) {
					jComboBoxTipus.setSelectedIndex(i);
					break;
				}
		}
		else {
			jLabelNomCentre.setText(gestor.getCentre().getNom());
			jLabelCodiCentre.setText(gestor.getCentre().getCodi());
		}
		updatePreRequisits();
	}

	/**
	 * This method initializes jPanelContainer
	 * 
	 * @return javax.swing.JPanel
	 */
	public JPanel getJPanelContainer(Dimension dimension) {
		if (jPanelContainer == null) {
			Insets defaultInset = new Insets(10, 10, 10, 10);
			
			GridBagConstraints gbcPanelAccions = new GridBagConstraints();
			gbcPanelAccions.gridx = 1;
			gbcPanelAccions.gridy = 7;
			gbcPanelAccions.fill = GridBagConstraints.HORIZONTAL;
			gbcPanelAccions.gridwidth = 2;
			gbcPanelAccions.insets = defaultInset;

			GridBagConstraints gbcPanelPreRequisits = new GridBagConstraints();
			gbcPanelPreRequisits.gridx = 1;
			gbcPanelPreRequisits.gridy = 6;
			gbcPanelPreRequisits.fill = GridBagConstraints.BOTH;
			gbcPanelPreRequisits.weightx = 1;
			gbcPanelPreRequisits.weighty = 1;
			gbcPanelPreRequisits.gridwidth = 2;
			gbcPanelPreRequisits.insets = defaultInset;
			
			JLabel jLabelEventsPreRequisits = new JLabel();
			jLabelEventsPreRequisits.setText(TDSLanguageUtils.getMessage("Client.PantallaCampsEvents.PreActuals"));
			GridBagConstraints gbcLabelPreRequisites = new GridBagConstraints();
			gbcLabelPreRequisites.gridx = 1;
			gbcLabelPreRequisites.gridy = 5;
			gbcLabelPreRequisites.gridwidth = 2;

			GridBagConstraints gbcPanelEvents = new GridBagConstraints();
			gbcPanelEvents.gridx = 1;
			gbcPanelEvents.gridy = 4;
			gbcPanelEvents.weightx = 1;
			gbcPanelEvents.weighty = 1;
			gbcPanelEvents.gridwidth = 2;
			gbcPanelEvents.fill = GridBagConstraints.BOTH;
			gbcPanelEvents.insets = defaultInset;
			
			JLabel jLabelEvents = new JLabel();
			jLabelEvents.setText(TDSLanguageUtils.getMessage("Client.PantallaCampsEvents.PrePossibles"));
			GridBagConstraints gbcLabelEvents = new GridBagConstraints();
			gbcLabelEvents.gridx = 1;
			gbcLabelEvents.gridy = 3;
			gbcLabelEvents.gridwidth = 2;
			
			GridBagConstraints gbcPanelCalendari = new GridBagConstraints();
			gbcPanelCalendari.gridx = 1;
			gbcPanelCalendari.gridy = 2;
			gbcPanelCalendari.fill = GridBagConstraints.NONE;
			gbcPanelCalendari.ipadx = 20;
			gbcPanelCalendari.ipady = 20;
			gbcPanelCalendari.anchor = GridBagConstraints.FIRST_LINE_START;
			gbcPanelCalendari.insets = defaultInset;
			
			JLabel jLabelSeleccioPrerequisits = new JLabel();
			jLabelSeleccioPrerequisits.setText(TDSLanguageUtils.getMessage("Client.PantallaCampsEvents.PreSeleccio"));
			GridBagConstraints gbcLabelSeleccioPrerequisits = new GridBagConstraints();
			gbcLabelSeleccioPrerequisits.gridx = 1;
			gbcLabelSeleccioPrerequisits.gridy = 1;
			gbcLabelSeleccioPrerequisits.gridwidth = 2;
			gbcLabelSeleccioPrerequisits.insets = new Insets(10, 10, 0, 10);

			GridBagConstraints gbcPanelFiltre = new GridBagConstraints();
			gbcPanelFiltre.gridx = 2;
			gbcPanelFiltre.gridy = 2;
			gbcPanelFiltre.insets = defaultInset;

			GridBagConstraints gbcPanelDadesEvent = new GridBagConstraints();
			gbcPanelDadesEvent.gridx = 0;
			gbcPanelDadesEvent.gridy = 1;
			gbcPanelDadesEvent.gridheight = 7;
			gbcPanelDadesEvent.anchor = GridBagConstraints.PAGE_START;
			gbcPanelDadesEvent.insets = defaultInset;

			GridBagConstraints gbcPanelTitol = new GridBagConstraints();
			gbcPanelTitol.gridx = 0;
			gbcPanelTitol.gridy = 0;
			gbcPanelTitol.gridwidth = 3;
			gbcPanelTitol.fill = GridBagConstraints.HORIZONTAL;
			gbcPanelTitol.anchor = GridBagConstraints.PAGE_START;
			gbcPanelTitol.insets = defaultInset;

			jPanelContainer = new JPanel();
			jPanelContainer.setLayout(new GridBagLayout());
			jPanelContainer.add(getJPanelTitol(), gbcPanelTitol);
			jPanelContainer.add(getJPanelDadesEvent(), gbcPanelDadesEvent);
			jPanelContainer.add(jLabelSeleccioPrerequisits, gbcLabelSeleccioPrerequisits);
			jPanelContainer.add(getJPanelCalendari(), gbcPanelCalendari);
			jPanelContainer.add(getJPanelFiltre(), gbcPanelFiltre);
			jPanelContainer.add(getJPanelEvents(), gbcPanelEvents);
			jPanelContainer.add(jLabelEvents, gbcLabelEvents);
			jPanelContainer.add(getJPanelPreRequisits(), gbcPanelPreRequisits);
			jPanelContainer.add(jLabelEventsPreRequisits, gbcLabelPreRequisites);
			jPanelContainer.add(getJPanelAccions(), gbcPanelAccions);
			// carrega les dades del event i/o centre
			initializeDades();
			// carrega les dades del calendari
			updateCalendar();
		}
		return jPanelContainer;
	}

	/**
	 * This method initializes jPanelFiltre
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelFiltre() {
		if (jPanelFiltre == null) {
			jPanelFiltre = new JPanelFiltreCalendari(this,
					JPanelFiltreCalendari.Disposicio.VERTICAL, gestor.getUniversitat(),
					gestor.getCentre());
		}
		return jPanelFiltre;
	}
	
	/**
	 * This method initializes jPanelEvents
	 * 
	 * @return JPanelEvents
	 */
	private JPanel getJPanelAccions() {
		if (jPanelAccions == null) {
			jPanelAccions = new JPanel();
			jPanelAccions.setLayout(new GridBagLayout());
			
			jButtonAfegir = new JButton();
			jButtonAfegir.setText(TDSLanguageUtils.getMessage("Client.PantallaCampsEvents.Afegir"));
			jButtonAfegir.setPreferredSize(dimButton);
			jButtonAfegir.setMinimumSize(dimButton);
			jButtonAfegir.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					int row = jPanelEvents.getSelectedRow();
					if(row >= 0) {
						Event ev = currentEvents.get(row);
						if(!preRequisits.contains(ev)) {
							preRequisits.addElement(ev);
							updatePreRequisits();
						}
						else
							new PantallaError("Client.PantallaCampsEvent.PrerequisitDupe", PantallaError.Tipus.WARNING);
					}
					else
						new PantallaError("Client.PantallaCampsEvent.SelecAfegir", PantallaError.Tipus.WARNING);
				}
			});
			
			GridBagConstraints gbcButtonAfegir = new GridBagConstraints();
			gbcButtonAfegir.gridx = 0;
			gbcButtonAfegir.gridy = 0;
			gbcButtonAfegir.insets = new Insets(0, 10, 0, 10);
			
			jButtonEliminar = new JButton();
			jButtonEliminar.setText(TDSLanguageUtils.getMessage("Client.PantallaCampsEvents.Eliminar"));
			jButtonEliminar.setPreferredSize(dimButton);
			jButtonEliminar.setMinimumSize(dimButton);
			jButtonEliminar.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					int row = jPanelPreRequisits.getSelectedRow();
					if(row >= 0) {
						preRequisits.remove(row);
						jPanelPreRequisits.removeSelectedRow(row);
					}
					else
						new PantallaError("Client.PantallaCampsEvent.SelecEliminar", PantallaError.Tipus.WARNING);
				}
			});
			GridBagConstraints gbcButtonEliminar = new GridBagConstraints();
			gbcButtonEliminar.gridx = 1;
			gbcButtonEliminar.gridy = 0;
			gbcButtonEliminar.insets = new Insets(0, 10, 0, 10);
			
			jPanelAccions.add(jButtonAfegir, gbcButtonAfegir);
			jPanelAccions.add(jButtonEliminar, gbcButtonEliminar);
		}
		return jPanelAccions;
	}

	/**
	 * This method initializes jPanelEvents
	 * 
	 * @return JPanelEvents
	 */
	private JPanelEvents getJPanelEvents() {
		if (jPanelEvents == null) {
			jPanelEvents = new JPanelEvents();
		}
		return jPanelEvents;
	}

	/**
	 * This method initializes jPanelTitol
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelTitol() {
		if (jPanelTitol == null) {
			Insets titleInset = new Insets(10, 15, 10, 10);
			Insets textInset = new Insets(10, 5, 10, 25);

			jLabelCodiCentre = new JLabel("");
			GridBagConstraints gbcCodiCentre = new GridBagConstraints();
			gbcCodiCentre.gridx = 3;
			gbcCodiCentre.gridy = 0;
			gbcCodiCentre.insets = textInset;

			jLabelTitolCodi = new JLabel();
			jLabelTitolCodi.setText(TDSLanguageUtils.getMessage("Client.PantallaCampsEvents.Codi"));
			GridBagConstraints gbcTitolCodi = new GridBagConstraints();
			gbcTitolCodi.gridx = 2;
			gbcTitolCodi.gridy = 0;
			gbcTitolCodi.insets = titleInset;

			jLabelNomCentre = new JLabel("");
			GridBagConstraints gbcNomCentre = new GridBagConstraints();
			gbcNomCentre.gridx = 1;
			gbcNomCentre.gridy = 0;
			gbcNomCentre.insets = textInset;

			jLabelTitolCentre = new JLabel(TDSLanguageUtils.getMessage("Client.PantallaCampsEvents.Centre"));
			GridBagConstraints gbcTitolCentre = new GridBagConstraints();
			gbcTitolCentre.gridx = 0;
			gbcTitolCentre.gridy = 0;
			gbcTitolCentre.insets = titleInset;

			jPanelTitol = new JPanel();
			jPanelTitol.setLayout(new GridBagLayout());
			jPanelTitol.setBorder(BorderFactory.createLineBorder(Color.GRAY));
			jPanelTitol.add(jLabelTitolCentre, gbcTitolCentre);
			jPanelTitol.add(jLabelNomCentre, gbcNomCentre);
			jPanelTitol.add(jLabelTitolCodi, gbcTitolCodi);
			jPanelTitol.add(jLabelCodiCentre, gbcCodiCentre);
		}
		return jPanelTitol;
	}

	/**
	 * This method initializes jPanelEvent
	 * Aquest JPanel recull els detalls de l'event 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelDadesEvent() {
		if (jPanelDadesEvent == null) {
			Insets defaultInset = new Insets(10, 5, 10, 5);
			GridBagConstraints gbcPanelAccionsEvent = new GridBagConstraints();
			gbcPanelAccionsEvent.gridx = 0;
			gbcPanelAccionsEvent.gridy = 9;
			gbcPanelAccionsEvent.gridwidth = 4; 
			gbcPanelAccionsEvent.insets = defaultInset;
			
			// Periode
			GridBagConstraints gbcTextDataPeriodeFi = new GridBagConstraints();
			gbcTextDataPeriodeFi.gridx = 3;
			gbcTextDataPeriodeFi.gridy = 8;
			gbcTextDataPeriodeFi.insets = defaultInset;

			GridBagConstraints gbcLabelDataPeriodeFi = new GridBagConstraints();
			gbcLabelDataPeriodeFi.gridx = 2;
			gbcLabelDataPeriodeFi.gridy = 8;
			gbcLabelDataPeriodeFi.insets = defaultInset;
			gbcLabelDataPeriodeFi.anchor = GridBagConstraints.EAST;

			GridBagConstraints gbcTextDataPeriodeInici = new GridBagConstraints();
			gbcTextDataPeriodeInici.gridx = 1;
			gbcTextDataPeriodeInici.gridy = 8;
			gbcTextDataPeriodeInici.insets = defaultInset;

			GridBagConstraints gbcLabelDataPeriodeInici = new GridBagConstraints();
			gbcLabelDataPeriodeInici.gridx = 0;
			gbcLabelDataPeriodeInici.gridy = 8;
			gbcLabelDataPeriodeInici.insets = defaultInset;
			gbcLabelDataPeriodeInici.anchor = GridBagConstraints.EAST;
			
			GridBagConstraints gbcLabelTitolPeriode = new GridBagConstraints();
			gbcLabelTitolPeriode.gridwidth = 4;
			gbcLabelTitolPeriode.gridx = 0;
			gbcLabelTitolPeriode.gridy = 7;
			gbcLabelTitolPeriode.insets = defaultInset;

			// Dates Event
			GridBagConstraints gbcTextLLindar = new GridBagConstraints();
			gbcTextLLindar.gridx = 3;
			gbcTextLLindar.gridy = 6;
			gbcTextLLindar.insets = defaultInset;

			GridBagConstraints gbcLabelLlindar = new GridBagConstraints();
			gbcLabelLlindar.gridx = 2;
			gbcLabelLlindar.gridy = 6;
			gbcLabelLlindar.insets = defaultInset;
			gbcLabelLlindar.anchor = GridBagConstraints.EAST;

			GridBagConstraints gbcTextDurada = new GridBagConstraints();
			gbcTextDurada.gridx = 1;
			gbcTextDurada.gridy = 6;
			gbcTextDurada.insets = defaultInset;

			GridBagConstraints gbcLabelDurada = new GridBagConstraints();
			gbcLabelDurada.gridx = 0;
			gbcLabelDurada.gridy = 6;
			gbcLabelDurada.insets = defaultInset;
			gbcLabelDurada.anchor = GridBagConstraints.EAST;
			// Dates Event
			GridBagConstraints gbcTextDataFi = new GridBagConstraints();
			gbcTextDataFi.gridx = 3;
			gbcTextDataFi.gridy = 5;
			gbcTextDataFi.insets = defaultInset;

			GridBagConstraints gbcLabelDataFi = new GridBagConstraints();
			gbcLabelDataFi.gridx = 2;
			gbcLabelDataFi.gridy = 5;
			gbcLabelDataFi.insets = defaultInset;
			gbcLabelDataFi.anchor = GridBagConstraints.EAST;

			GridBagConstraints gbcTextDataInici = new GridBagConstraints();
			gbcTextDataInici.gridx = 1;
			gbcTextDataInici.gridy = 5;
			gbcTextDataInici.insets = defaultInset;

			GridBagConstraints gbcLabelDataInici = new GridBagConstraints();
			gbcLabelDataInici.gridx = 0;
			gbcLabelDataInici.gridy = 5;
			gbcLabelDataInici.insets = defaultInset;
			gbcLabelDataInici.anchor = GridBagConstraints.EAST;

			GridBagConstraints gbcLabelTitolDates = new GridBagConstraints();
			gbcLabelTitolDates.gridwidth = 4;
			gbcLabelTitolDates.gridx = 0;
			gbcLabelTitolDates.gridy = 4;
			gbcLabelTitolDates.insets = defaultInset;
			// Places - Preu
			GridBagConstraints gbcTextPreu = new GridBagConstraints();
			gbcTextPreu.gridx = 3;
			gbcTextPreu.gridy = 3;
			gbcTextPreu.insets = defaultInset;

			GridBagConstraints gbcLabelPreu = new GridBagConstraints();
			gbcLabelPreu.gridx = 2;
			gbcLabelPreu.gridy = 3;
			gbcLabelPreu.insets = defaultInset;
			gbcLabelPreu.anchor = GridBagConstraints.EAST;

			GridBagConstraints gbcTextPlaces = new GridBagConstraints();
			gbcTextPlaces.gridx = 1;
			gbcTextPlaces.gridy = 3;
			gbcTextPlaces.insets = defaultInset;

			GridBagConstraints gbcLabelPlaces = new GridBagConstraints();
			gbcLabelPlaces.gridx = 0;
			gbcLabelPlaces.gridy = 3;
			gbcLabelPlaces.insets = defaultInset;
			gbcLabelPlaces.anchor = GridBagConstraints.EAST;

			// Tipus
			GridBagConstraints gbcLabelTipus = new GridBagConstraints();
			gbcLabelTipus.gridx = 0;
			gbcLabelTipus.gridy = 2;
			gbcLabelTipus.insets = defaultInset;
			gbcLabelTipus.anchor = GridBagConstraints.EAST;

			GridBagConstraints gbcComboTipus = new GridBagConstraints();
			gbcComboTipus.gridx = 1;
			gbcComboTipus.gridy = 2;
			gbcComboTipus.gridwidth = 3;
			gbcComboTipus.insets = defaultInset;
			// Nom
			GridBagConstraints gbcTextNom = new GridBagConstraints();
			gbcTextNom.gridx = 1;
			gbcTextNom.gridy = 1;
			gbcTextNom.gridwidth = 3;
			gbcTextNom.insets = defaultInset;
			gbcTextNom.fill = GridBagConstraints.BOTH;

			GridBagConstraints gbcLabelNom = new GridBagConstraints();
			gbcLabelNom.gridx = 0;
			gbcLabelNom.gridy = 1;
			gbcLabelNom.insets = defaultInset;
			gbcLabelNom.anchor = GridBagConstraints.EAST;

			GridBagConstraints gbcLabelTitol = new GridBagConstraints();
			gbcLabelTitol.gridwidth = 4;
			gbcLabelTitol.gridx = 0;
			gbcLabelTitol.gridy = 0;
			gbcLabelTitol.insets = defaultInset;

			jLabelEventTitol = new JLabel();
			jLabelEventTitol.setText(TDSLanguageUtils.getMessage("Client.PantallaCampsEvents.Detalls"));
			jLabelNomEvent = new JLabel();
			jLabelNomEvent.setText(TDSLanguageUtils.getMessage("Client.PantallaCampsEvents.Nom"));
			jLabelTipus = new JLabel();
			jLabelTipus.setText(TDSLanguageUtils.getMessage("Client.PantallaCampsEvents.Tipus"));
			jLabelPlaces = new JLabel();
			jLabelPlaces.setText(TDSLanguageUtils.getMessage("Client.PantallaCampsEvents.Places"));
			jLabelPreu = new JLabel();
			jLabelPreu.setText(TDSLanguageUtils.getMessage("Client.PantallaCampsEvents.Preu"));
			jLabelTitolDates = new JLabel();
			jLabelTitolDates.setText(TDSLanguageUtils.getMessage("Client.PantallaCampsEvents.Dates"));
			jLabelDataInici = new JLabel();
			jLabelDataInici.setText(TDSLanguageUtils.getMessage("Client.PantallaCampsEvents.Inici"));
			jLabelDataFi = new JLabel();
			jLabelDataFi.setText(TDSLanguageUtils.getMessage("Client.PantallaCampsEvents.Fi"));
			jLabelLlindar = new JLabel();
			jLabelLlindar.setText(TDSLanguageUtils.getMessage("Client.PantallaCampsEvents.Llindar"));
			jLabelDurada = new JLabel();
			jLabelDurada.setText(TDSLanguageUtils.getMessage("Client.PantallaCampsEvents.Durada"));
			jLabelTitolPeriode = new JLabel();
			jLabelTitolPeriode.setText(TDSLanguageUtils.getMessage("Client.PantallaCampsEvents.Periode"));
			jLabelDataPeriodeInici = new JLabel();
			jLabelDataPeriodeInici.setText(TDSLanguageUtils.getMessage("Client.PantallaCampsEvents.Inici"));
			jLabelDataPeriodeFi = new JLabel();
			jLabelDataPeriodeFi.setText(TDSLanguageUtils.getMessage("Client.PantallaCampsEvents.Fi"));
			jPanelDadesEvent = new JPanel();
			jPanelDadesEvent.setLayout(new GridBagLayout());
			jPanelDadesEvent.add(jLabelEventTitol, gbcLabelTitol);
			jPanelDadesEvent.add(jLabelNomEvent, gbcLabelNom);
			jPanelDadesEvent.add(getJTextFieldNomEvent(), gbcTextNom);
			jPanelDadesEvent.add(jLabelTipus, gbcLabelTipus);
			jPanelDadesEvent.add(getJComboBoxTipus(), gbcComboTipus);
			jPanelDadesEvent.add(jLabelPlaces, gbcLabelPlaces);
			jPanelDadesEvent.add(getJTextFieldPlaces(), gbcTextPlaces);
			jPanelDadesEvent.add(jLabelPreu, gbcLabelPreu);
			jPanelDadesEvent.add(getJTextFieldPreu(), gbcTextPreu);
			jPanelDadesEvent.add(jLabelTitolDates, gbcLabelTitolDates);
			jPanelDadesEvent.add(jLabelDataInici, gbcLabelDataInici);
			jPanelDadesEvent.add(getJTextFieldDataInici(), gbcTextDataInici);
			jPanelDadesEvent.add(jLabelDataFi, gbcLabelDataFi);
			jPanelDadesEvent.add(getJTextFieldDataFi(), gbcTextDataFi);
			jPanelDadesEvent.add(jLabelDurada, gbcLabelDurada);
			jPanelDadesEvent.add(getJTextFieldDurada(), gbcTextDurada);
			jPanelDadesEvent.add(jLabelLlindar, gbcLabelLlindar);
			jPanelDadesEvent.add(getJTextFieldLlindar(), gbcTextLLindar);
			jPanelDadesEvent.add(jLabelTitolPeriode, gbcLabelTitolPeriode);
			jPanelDadesEvent.add(jLabelDataPeriodeInici, gbcLabelDataPeriodeInici);
			jPanelDadesEvent.add(getJTextFieldDataPeriodeInici(), gbcTextDataPeriodeInici);
			jPanelDadesEvent.add(jLabelDataPeriodeFi, gbcLabelDataPeriodeFi);
			jPanelDadesEvent.add(getJTextFieldDataPeriodeFi(), gbcTextDataPeriodeFi);
			jPanelDadesEvent.add(getJPanelAccionsEvent(), gbcPanelAccionsEvent);
		}
		return jPanelDadesEvent;
	}
	
	/**
	 * This method initializes jPanelAccions	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelAccionsEvent() {
		if (jPanelAccionsEvent == null) {
			Insets defaultInset = new Insets(10, 10, 10, 10);

			// Botons
			GridBagConstraints gbcButtonGuardar = new GridBagConstraints();
			gbcButtonGuardar.gridy = 0;
			gbcButtonGuardar.insets = defaultInset;
			
			GridBagConstraints gbcButtonCancelar = new GridBagConstraints();
			gbcButtonCancelar.gridx = 1;
			gbcButtonCancelar.gridy = 0;
			gbcButtonCancelar.insets = defaultInset;
			
			jPanelAccionsEvent = new JPanel();
			jPanelAccionsEvent.setLayout(new GridBagLayout());
			jPanelAccionsEvent.add(getJButtonGuardarEvent(), gbcButtonGuardar);
			jPanelAccionsEvent.add(getJButtonCancelar(), gbcButtonCancelar);
		}
		return jPanelAccionsEvent;
	}

	/**
	 * This method initializes
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanelEvents getJPanelPreRequisits() {
		if (jPanelPreRequisits == null) {
			jPanelPreRequisits = new JPanelEvents();
		}
		return jPanelPreRequisits;
	}

	/**
	 * This method initializes jPanelCalendari
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelCalendari() {
		if (jPanelCalendari == null) {
			jPanelCalendari = new JPanelCalendari(this, new Dimension(200, 170));
		}
		return jPanelCalendari;
	}
	
	/**
	 * This method initializes jButtonGuardarEvent
	 * @return
	 */
	private JButton getJButtonCancelar() {
		if(jButtonCancelar == null) {
			jButtonCancelar = new JButton();
			jButtonCancelar.setText(TDSLanguageUtils.getMessage("Client.PantallaCampsEvents.Cancelar"));
			jButtonCancelar.setPreferredSize(dimButton);
			jButtonCancelar.setMinimumSize(dimButton);
			jButtonCancelar.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					gestor.carregaPantalla(GestorPantallaSS2.TipusPantalla.llistar, null);
				}
			});
		}
		return jButtonCancelar;
	}
	
	/**
	 * This method initializes jButtonGuardarEvent
	 * @return
	 */
	private JButton getJButtonGuardarEvent() {
		if(jButtonGuardarEvent == null) {
			jButtonGuardarEvent = new JButton();
			if(event == null)
				jButtonGuardarEvent.setText(TDSLanguageUtils.getMessage("Client.PantallaCampsEvents.Desar"));
			else
				jButtonGuardarEvent.setText(TDSLanguageUtils.getMessage("Client.PantallaCampsEvents.Modificar"));
			jButtonGuardarEvent.setPreferredSize(dimButton);
			jButtonGuardarEvent.setMinimumSize(dimButton);
			jButtonGuardarEvent.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// s'actualitza/crea l'event i en cas d'exit s'actualiza al servidor
					if(actualitzarEvent())
						try {
							boolean esInsert = event.getCodi() == 0;
							
							if(esInsert)
								event.setActiu(true);
							
							event.setCodi(remot.insertUpdateEvent(event, preRequisits));
							
							if(esInsert)
								new PantallaError("Client.PantallaCampsEvent.InsertOK", PantallaError.Tipus.INFO);
							else
								new PantallaError("Client.PantallaCampsEvent.UpdateOK", PantallaError.Tipus.INFO);
							//torna al llistat d'events
							gestor.carregaPantalla(GestorPantallaSS2.TipusPantalla.llistar, null);
						} catch (RemoteException e) {
							new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
						} catch (GestioEventsException e) {
							new PantallaError(e.getMessage(), PantallaError.Tipus.ERROR);
						}
				}
			});
		}
		return jButtonGuardarEvent;
	}

	/**
	 * Valida les dades abans i crea el nou event
	 * en cas d'exit
	 */
	protected boolean actualitzarEvent() {
		DateFormat fmt  = new SimpleDateFormat("dd/MM/yyyy");
		String errorCode = "";

		// Parsejar valors
		int places = 0;
		try {
			places = Integer.parseInt(jTextFieldPlaces.getText());
		} catch (NumberFormatException e) {
			errorCode= "Client.PantallaCampsEvent.Places";
		}
		
		float preu = 0;
		if(errorCode.equals(""))
			try {
				preu = Float.valueOf(jTextFieldPreu.getText());
			} catch (NumberFormatException e) {
				errorCode= "Client.PantallaCampsEvent.Preu";
			}
		
		Date iniciEvent = null;
		if(errorCode.equals(""))
			try {
				iniciEvent = fmt.parse(jTextFieldDataInici.getText());
			} catch (ParseException e) {
				errorCode= "Client.PantallaCampsEvent.DataInici";
			}
		
		Date fiEvent = null;
		if(errorCode.equals(""))
			try {
				fiEvent = fmt.parse(jTextFieldDataFi.getText());
			} catch (ParseException e) {
				errorCode= "Client.PantallaCampsEvent.DataFi";
			}
		
		int durada = 0;
		if(errorCode.equals(""))
			try {
				durada = Integer.parseInt(jTextFieldDurada.getText());
			} catch (NumberFormatException e) {
				errorCode= "Client.PantallaCampsEvent.Durada";
			}
		
		int llindar = 0;
		if(errorCode.equals(""))
			try {
				llindar = Integer.parseInt(jTextFieldLlindar.getText());
			} catch (NumberFormatException e) {
				errorCode= "Client.PantallaCampsEvent.Llindar";
			}
		
		Date IniciInscripcio = null;
		if(errorCode.equals(""))
			try {
				IniciInscripcio = fmt.parse(jTextFieldDataPeriodeInici.getText());
			} catch (ParseException e) {
				errorCode= "Client.PantallaCampsEvent.DataIniciInscripcio";
			}
		
		Date fiInscripcio = null;
		if(errorCode.equals(""))
			try {
				fiInscripcio = fmt.parse(jTextFieldDataPeriodeFi.getText());
			} catch (ParseException e) {
				errorCode= "Client.PantallaCampsEvent.DataFiInscripcio";
			}

		if(!errorCode.equals("")) {
			new PantallaError(errorCode, PantallaError.Tipus.ERROR);
			return false;
		}
		
		// Crea o actualitza l'event
		if(event == null)
			event = new Event();
		
		event.setCentre(gestor.getCentre());
		event.setDescripcio(jTextFieldNomEvent.getText());
		event.setTipusevent((TipusEvent)jComboBoxTipus.getSelectedItem());
		event.setCapacitat(places);
		event.setPreu(preu);
		event.setDataInici(iniciEvent);
		event.setDataFi(fiEvent);
		event.setDurada(durada);
		event.setPercentatge(llindar);
		event.setInscripInici(IniciInscripcio);
		event.setInscripFi(fiInscripcio);
		
		return true;
	}

	/**
	 * This method initializes jTextFieldNomEvent
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextFieldNomEvent() {
		if (jTextFieldNomEvent == null) {
			jTextFieldNomEvent = new JTextField();
			jTextFieldNomEvent
					.setMinimumSize(new Dimension(200, defaultHeight));
			jTextFieldNomEvent.setPreferredSize(new Dimension(200,
					defaultHeight));
		}
		return jTextFieldNomEvent;
	}

	/**
	 * This method initializes jComboBoxTipus
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBoxTipus() {
		if (jComboBoxTipus == null) {
			jComboBoxTipus = new JComboBox();
			jComboBoxTipus.setMinimumSize(new Dimension(200, defaultHeight));
			jComboBoxTipus.setPreferredSize(new Dimension(200, defaultHeight));
			Vector<TipusEvent> tipusEvents = null;
			try {
				tipusEvents = remot.getTipusEvents(gestor.getCentre(), true);
			} catch (RemoteException e) {
				new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
			} catch (GestioEventsException e) {
				new PantallaError(e.getMessage(), PantallaError.Tipus.ERROR);
			}

			Helpers.setItemsCombo(tipusEvents, jComboBoxTipus);
		}
		return jComboBoxTipus;
	}
	
	/**
	 * This method initializes jTextFieldPlaces
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextFieldPlaces() {
		if (jTextFieldPlaces == null) {
			jTextFieldPlaces = new JTextField();
			jTextFieldPlaces.setMinimumSize(new Dimension(80, defaultHeight));
			jTextFieldPlaces.setPreferredSize(new Dimension(80,
					defaultHeight));
		}
		return jTextFieldPlaces;
	}

	/**
	 * This method initializes getJTextFieldPreu
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextFieldPreu() {
		if (jTextFieldPreu == null) {
			jTextFieldPreu = new JTextField();
			jTextFieldPreu.setMinimumSize(new Dimension(80, defaultHeight));
			jTextFieldPreu.setPreferredSize(new Dimension(80, defaultHeight));
		}
		return jTextFieldPreu;
	}
	
	
	/**
	 * This method initializes jTextFieldDataPeriodeInici
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextFieldDataPeriodeInici() {
		if (jTextFieldDataPeriodeInici == null) {
			jTextFieldDataPeriodeInici = new JTextField();
			jTextFieldDataPeriodeInici.setMinimumSize(new Dimension(80, defaultHeight));
			jTextFieldDataPeriodeInici.setPreferredSize(new Dimension(80,
					defaultHeight));
		}
		return jTextFieldDataPeriodeInici;
	}

	/**
	 * This method initializes jTextFieldPeriodeFi
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextFieldDataPeriodeFi() {
		if (jTextFieldDataPeriodeFi == null) {
			jTextFieldDataPeriodeFi = new JTextField();
			jTextFieldDataPeriodeFi.setMinimumSize(new Dimension(80, defaultHeight));
			jTextFieldDataPeriodeFi.setPreferredSize(new Dimension(80, defaultHeight));
		}
		return jTextFieldDataPeriodeFi;
	}
	
	

	/**
	 * This method initializes jTextFieldDataInici
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextFieldDataInici() {
		if (jTextFieldDataInici == null) {
			jTextFieldDataInici = new JTextField();
			jTextFieldDataInici.setMinimumSize(new Dimension(80, defaultHeight));
			jTextFieldDataInici.setPreferredSize(new Dimension(80,
					defaultHeight));
		}
		return jTextFieldDataInici;
	}

	/**
	 * This method initializes jTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextFieldDataFi() {
		if (jTextFieldDataFi == null) {
			jTextFieldDataFi = new JTextField();
			jTextFieldDataFi.setMinimumSize(new Dimension(80, defaultHeight));
			jTextFieldDataFi.setPreferredSize(new Dimension(80, defaultHeight));
		}
		return jTextFieldDataFi;
	}

	/**
	 * This method initializes jTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextFieldDurada() {
		if (jTextFieldDurada == null) {
			jTextFieldDurada = new JTextField();
			jTextFieldDurada.setMinimumSize(new Dimension(80, defaultHeight));
			jTextFieldDurada.setPreferredSize(new Dimension(80, defaultHeight));
		}
		return jTextFieldDurada;
	}

	/**
	 * This method initializes jTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextFieldLlindar() {
		if (jTextFieldLlindar == null) {
			jTextFieldLlindar = new JTextField();
			jTextFieldLlindar.setMinimumSize(new Dimension(80, defaultHeight));
			jTextFieldLlindar.setPreferredSize(new Dimension(80, defaultHeight));
		}
		return jTextFieldLlindar;
	}
	
	/**
	 * Redibuixa la llista d'events prerequisits
	 */
	private void updatePreRequisits()
	{
		if(preRequisits != null && !preRequisits.isEmpty())
			jPanelPreRequisits.drawEvents(preRequisits);
	}

	@Override
	public void updateCalendar() {
		Date dataInici = null;
		Date dataFi = null;
		try {
			dataInici = jPanelCalendari.getDateInici();
			dataFi = jPanelCalendari.getDateFinal();
		} catch (ParseException e) {
			new PantallaError("Client.errorIntern", PantallaError.Tipus.ERROR);
		}
		// crida al servidor per rebre els events
		currentEvents = null;
		try {
			currentEvents = remot.getEvents(jPanelFiltre.getUniversitat(),
					jPanelFiltre.getCentre(), dataInici, dataFi, true);
		} catch (RemoteException e) {
			new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
		} catch (GestioEventsException e) {
			
		}
		jPanelEvents.drawEvents(currentEvents);
		jPanelCalendari.drawCalendar(currentEvents);
	}
}
